<?php

namespace app\common\model;

use think\Model;
use think\Db;

/**
 * 砍价模型
 */
class Cutdown extends Model
{

    protected $append = [
        'zuigao_text'
    ];
    public function getZuiGaoTextAttr($value,$data){
        return ($data['price']*100 - $data['min_price']*100)/100;
    }

    public function addCutdownList($user_id, $cutdown_follow_id, $is_first = 0,$first_cut_price = 0)
    {

        $cutdownFollowData = Db::name('cutdown_follow')->find($cutdown_follow_id);
        $cutdown = self::get($cutdownFollowData['cutdown_id']);


        $randprice = rand($cutdown['min_cut_price'] * 100, $cutdown['max_cut_price'] * 100) / 100;
        $randprice = number_format($randprice, 2, '.', '');
        $is_first ? $cut_price = $first_cut_price : $cut_price = $randprice;



        if($cut_price*100 >= ($cutdownFollowData['deal_price'] * 100 - $cutdown['min_price'] * 100)){
            $cut_price = ($cutdownFollowData['deal_price'] * 100 - $cutdown['min_price'] * 100)/100;
            Db::name('cutdown_follow')->where('id',$cutdown_follow_id)->update(['status'=>1]);
        }

        //已经达到最低价 就不能砍了
        if($cutdownFollowData['deal_price'] == $cutdown['min_price']){

            return ['code' => 0, 'msg' => '已经砍到最低价了','data' => ''];
        }
        $res = Db::name('cutdown_list')->insert([
            'cutdown_follow_id' => $cutdown_follow_id,
            'user_id' => $user_id,
            'createtime' => time(),
            'cut_price' => $cut_price

        ]);

        if ($res) {
            $cutdownList = Db::name('cutdown_list')->alias('cl')
                ->field('cl.cut_price,u.nickname,u.avatar')
                ->join('user u', 'cl.user_id = u.id')
                ->where(['cl.cutdown_follow_id' => $cutdown_follow_id])
                ->order('cl.cut_price desc')
                ->select();

            $ret = Db::name('cutdown')->alias('c')
                ->join('goods g', 'c.goods_id = g.id')
                ->field('c.id as cutdown_id,c.goods_id,c.item_id,c.description,c.sales_sum,c.price,g.image')
                ->where('c.id', $cutdownFollowData['cutdown_id'])
                ->find();

            $ret['cutdown_follow_id'] = $cutdown_follow_id;
            if($first_cut_price){
                $ret['yikan'] = ($cutdown['price'] * 100 - $cutdownFollowData['deal_price'] * 100) / 100;
                $ret['haikekan'] = ($cutdownFollowData['deal_price'] * 100 - $cutdown['min_price'] * 100) / 100;
            }else{
                $ret['yikan'] = ($cutdown['price'] * 100 - ($cutdownFollowData['deal_price'] * 100 - $cut_price*100)) / 100;
                $ret['haikekan'] = (($cutdownFollowData['deal_price'] * 100 - $cut_price*100) - $cutdown['min_price'] * 100) / 100;
            }

            $ret['time_limit'] = $cutdown['time_limit'];
            $ret['cut_count'] = $cutdownFollowData['cut_count'];
            $ret['cutdownList'] = $cutdownList;
            $ret['cut_price'] = $cut_price;


            return ['code' => 1, 'msg' => '', 'data' => $ret];
        } else {
            return ['code' => 0, 'msg' => '写入砍价记录表失败','data' => ''];

        }

    }

    public function goods(){
        return $this->belongsTo('Goods','goods_id','id');
    }





}